Package gwtappcontainer.server.apps.content

Source Code of gwtappcontainer.server.apps.content.ContentAPI

package gwtappcontainer.server.apps.content;

import gwtappcontainer.server.apis.admin.AdminAPI;
import gwtappcontainer.server.apis.admin.Roles;
import gwtappcontainer.server.apis.admin.Roles.Role;
import gwtappcontainer.server.apps.APIBase;
import gwtappcontainer.server.apps.APIException;
import gwtappcontainer.shared.apis.APIResponse;
import gwtappcontainer.shared.apis.APIResponse.Status;
import gwtappcontainer.shared.apis.admin.RoleProp;
import gwtappcontainer.shared.apps.content.ContentProp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.annotation.Nullable;
import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;
import com.google.gson.Gson;

@Api (name = "content",
  scopes = { "https://www.googleapis.com/auth/userinfo.email" }
)
public class ContentAPI extends APIBase {
 
  private Logger logger = Logger.getLogger(ContentAPI.class.getName());
   
  @ApiMethod(path="setcontent", httpMethod = HttpMethod.PUT)
  public APIResponse setContent(@Named("content_html") String html,
      @Named("tag") String tag, @Named("publish") boolean publish,
      User user) {
   
    try {             
      //content admin for this tag or portal admin can set content
      String contentAdminRole = getContentAdminRoleForTag(tag);
      gateKeeper.ensureRole(user, contentAdminRole,
          Role.PORTAL_ADMIN.toString());
     
      //ensure tag is valid
      if (false == isValidTag(tag))
        throw new APIException(Status.ERROR_RESOURCE_DOES_NOT_EXIST,
            "[" + tag + "] is not valid");
           
      ContentRepository repository = new ContentRepository();
     
      ContentProp prop = repository.setContent(html, tag, publish);               
     
      return new APIResponse(Status.SUCCESS, prop);
    } catch (Exception ex) {
      return new APIResponse(ex);
    }
  }
 
  @ApiMethod(path="setlargecontent", httpMethod = HttpMethod.PUT)
  public APIResponse setLargeContent(@Named("tag") String tag,
      @Named("publish") boolean publish,
      @Nullable @Named("html_part_1") String html1,
      @Nullable @Named("html_part_2") String html2,
      @Nullable @Named("html_part_3") String html3,
      @Nullable @Named("html_part_4") String html4,
      @Nullable @Named("html_part_5") String html5,
      @Nullable @Named("html_part_6") String html6,
      @Nullable @Named("html_part_7") String html7,
      @Nullable @Named("html_part_8") String html8,
      @Nullable @Named("html_part_9") String html9,     
      User user) {
   
    try {
      StringBuilder sb = new StringBuilder();
     
      if (null != html1) sb.append(html1);
      if (null != html2) sb.append(html2);
      if (null != html3) sb.append(html3);
      if (null != html4) sb.append(html4);
      if (null != html5) sb.append(html5);
      if (null != html6) sb.append(html6);
      if (null != html7) sb.append(html7);
      if (null != html8) sb.append(html8);
      if (null != html9) sb.append(html9);
     
      String content = sb.toString();
     
      if (content.equals(""))
        throw new APIException(Status.ERROR_RESOURCE_INCORRECTLY_SPECIFIED,
            "content cannot be empty");
     
      APIResponse response = setContent(content, tag, publish, user);
      if (response.statusCode != Status.SUCCESS)
        throw new APIException(response.statusCode, response.userFriendlyMessage);
               
      return response;
     
    } catch (Exception ex) {
      return new APIResponse(ex);
   
  }
 
  @ApiMethod(path="changepublishstatus", httpMethod = HttpMethod.PUT)
  public APIResponse setPublishStatus(@Named("tag") String tag,
      @Named("publish") boolean publish, User user) {

    try {
      //content admin for this tag or portal admin can set content
      String contentAdminRole = getContentAdminRoleForTag(tag);
      gateKeeper.ensureRole(user, contentAdminRole,
          Role.PORTAL_ADMIN.toString());       
           
      ContentRepository repository = new ContentRepository();   
      ContentProp prop = repository.setPublishStatus(tag, publish);
     
      return new APIResponse(Status.SUCCESS, prop);
    } catch (Exception ex) {
      return new APIResponse(ex);
    }
   
  }
 
  @ApiMethod(path="getcontent", httpMethod = HttpMethod.GET)
  public APIResponse getContent(@Named("tag") String tag, User user) {
   
    try {
      ContentRepository repository = new ContentRepository();
      ContentProp prop = repository.getContent(tag);
           
      logger.info("received getContent for tag - " + tag);
     
      if (null == prop)
        throw new APIException(Status.ERROR_RESOURCE_DOES_NOT_EXIST,
            "Either tag [" + tag +
            "] is invalid or there is no content for this tag");
     
      //if publish is true anyone can view content, otherwise only valid users
      if (prop.publish == false)
        gateKeeper.ensureValidUser(user);
           
      logger.info("returning content - " + new Gson().toJson(prop));
                       
      return new APIResponse(Status.SUCCESS, prop)
    } catch (Exception ex) {
      return new APIResponse(ex);
    }
  }   
 
  @ApiMethod(path="assignuserascontentadmin", httpMethod = HttpMethod.PUT)
  public APIResponse assignUserAsContentAdmin(@Named("email") String email,
      @Named("tag") String tag, User user) {
     
    try {
      AdminAPI adminAPI = new AdminAPI();
      String role = getContentAdminRoleForTag(tag);
     
      APIResponse resp = adminAPI.assignRoleToUser(email, role, user);
     
      return resp; 
    } catch (Exception ex) {
      return new APIResponse(ex);
    }
  }
 
  @ApiMethod(path="addnewtag", httpMethod = HttpMethod.PUT)
  public APIResponse addNewTag(@Named("content_tag") String tag, User user) {
    try {       
      //only DEVELOPER can add tag
      ensureRole(user, Role.DEVELOPER)
     
      AdminAPI adminAPI = new AdminAPI();
      String role = getContentAdminRoleForTag(tag);
      APIResponse resp = adminAPI.addRole(role, user);
     
      return resp;
    } catch (Exception ex) {
      return new APIResponse(ex);
    }
  }
 
  @ApiMethod(path="getalltags", httpMethod = HttpMethod.GET)
  public APIResponse getAllTags() {
    try {
      AdminAPI adminAPI = new AdminAPI();
      APIResponse resp = adminAPI.getAllRoles();
     
      @SuppressWarnings("unchecked")
      List<RoleProp> roles = (List<RoleProp>) resp.object;
     
      List<String> tags = getTagsFromRoles(roles);
     
      resp.statusCode = Status.SUCCESS;
      resp.object = tags;
      resp.userFriendlyMessage = "No of tags = " + tags.size();
     
      return resp;
    } catch (Exception ex) {
      return new APIResponse(ex);
    }
  }
 
  @ApiMethod(path="getallpublishedcontents", httpMethod = HttpMethod.GET)
  public APIResponse getAllValidPublishedContents() {
    try {
      ContentRepository repository = new ContentRepository();
     
      Map<String, String> published = repository.getAllValidPublishedContents();
     
      APIResponse response = new APIResponse(Status.SUCCESS, published);
     
      return response; 
    } catch (Exception ex) {
      return new APIResponse(ex);
    }
  }
 
  private boolean isValidTag(String tag) {
    APIResponse resp = getAllTags();
    if (resp.statusCode != Status.SUCCESS)
      return false;
   
    @SuppressWarnings("unchecked")
    List<String> roles = (List<String>) resp.object;
   
    boolean valid = roles.contains(tag.toUpperCase());
   
    return valid;     
  }
 
  private String getContentAdminRoleForTag(String tag) {
    String role = Roles.CONTENT_ADMIN_PREFIX + tag.toUpperCase();
    return role;
  }
 
  private List<String> getTagsFromRoles(List<RoleProp> roles) {
    List<String> tags = new ArrayList<String>();
    for (RoleProp roleProp : roles) {
      String prefix = Roles.CONTENT_ADMIN_PREFIX.toUpperCase();
      if (roleProp.name.startsWith(prefix)) {
        String tag = roleProp.name.substring(prefix.length());
        tags.add(tag);
      }
    }
   
    return tags;
  }
}
TOP

Related Classes of gwtappcontainer.server.apps.content.ContentAPI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.